package com.topscomm.tap.common;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisMmsStore {
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	public static final String REDIS_KEY_PREFIX = "mms:";
	private long expireHour = 12;

	/**
	 * 
	 * 
	 * @description:缓存数据
	 * @param key
	 * @param value
	 * @author: jiangtao1
	 * @date: 2021年6月16日上午9:39:27
	 * @modify:
	 */
	public void storeData(String key, List<Map<String, Object>> value) {
		String fullKey = REDIS_KEY_PREFIX + key;
		redisTemplate.opsForValue().set(fullKey, value, expireHour, TimeUnit.HOURS);
	}

	/**
	 * 
	 * 
	 * @description:查询数据
	 * @param key
	 * @return
	 * @author: jiangtao1
	 * @date: 2021年6月16日上午9:39:48
	 * @modify:
	 */
	public List<Map<String, Object>> queryData(String key) {
		String fullKey = REDIS_KEY_PREFIX + key;
		Object value = redisTemplate.opsForValue().get(fullKey);
		if (value == null)
			return null;
		try {
			List<Map<String, Object>> trueValue = (List<Map<String, Object>>) value;
			return trueValue;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 
	 * 
	 * @description:查询该key是否存在
	 * @param key
	 * @return
	 * @author: jiangtao1
	 * @date: 2021年6月16日上午9:40:31
	 * @modify:
	 */
	public boolean existKey(String key) {
		String fullKey = REDIS_KEY_PREFIX + key;
		return redisTemplate.hasKey(fullKey);
	}

	/**
	 * 
	 * @description:删除key
	 * @param key
	 * @author: jiangtao1
	 * @date: 2021年6月16日上午9:40:58
	 * @modify:
	 */
	public void removeKey(String key) {
		String fullKey = REDIS_KEY_PREFIX + key;
		redisTemplate.delete(fullKey);
	}

	/**
	 * 
	 * @description:清除key
	 * @author: jiangtao1
	 * @date: 2021年6月16日上午9:41:45
	 * @modify:
	 */
	public void clearData() {
		Set<String> keys = redisTemplate.keys(REDIS_KEY_PREFIX + "*");
		if (keys != null) {
			for (String key : keys) {
				redisTemplate.delete(key);
			}
		}
	}
}
